<!--  组织架构 -->
<template>
  <div class="department-container">
    <div class="app-container">
          <AddorEdit v-if="showDialog" 
          :visible="showDialog"
          @close="guanbidanchuang"
          :origin-list='originList' 
          :id="danchuangId" 
          :is-edit="isEdit" />
          <!-- 还要传一个新增还是编辑的状态 -->
      <el-card>
          <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
            <el-col :span="20">
              <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
            </el-col>
            <el-col :span="4">
              <el-row type="flex" justify="end">
                <!-- 两个内容 -->
                <el-col>负责人</el-col>
                <!-- 下拉菜单 element -->
                <el-col>
                  <el-dropdown>
                    <span>
                      操作<i class="el-icon-arrow-down" />
                    </span>
                    <el-dropdown-menu slot="dropdown">
                      <el-dropdown-item @click.native="addzibumen('')">添加子部门</el-dropdown-item>
                    </el-dropdown-menu>
                  </el-dropdown>
                </el-col>
              </el-row>
            </el-col>
          </el-row>
          
          <el-tree
    :data="list"
    :default-expand-all="true"
  >
    <!-- 作用域插槽 data拿到的是每一个子节点的对象 -->
    <!-- 
      #default === slot-scope
      什么时候用到作用域插槽？父组件中如果想使用子组件中的数据进行自定义内容的渲染 (类似于table 单元格数据渲染)
    -->
    <!-- 1.插槽  在组件内<slot>默认内容</slot>  <组件标签>传递的结构</组件标签>
            2.具名插槽   在组件内<slot name='title'>默认内容</slot>    <组件标签 v-slot:title>传递的结构</组件标签> 简写#
    -->
    <template #default="{ data }">
      <el-row
        type="flex"
        justify="space-between"
        align="middle"
        style="height: 40px; width: 100%;"
      >
        <el-col :span="20">
          <span>{{ data.name }}</span>
        </el-col>
        <el-col :span="4">
          <el-row type="flex" justify="end">
            <!-- 两个内容 -->
            <el-col>{{ data.manager }}</el-col>
            <el-col>
              <!-- 下拉菜单 element -->
              <el-dropdown>
                <span> 操作<i class="el-icon-arrow-down" /> </span>
                <!-- 下拉菜单 -->
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item @click.native="addzibumen(data.id)" >添加子部门</el-dropdown-item>
                  <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                  <el-dropdown-item @click.native="hDel(data.id)">删除部门</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </el-col>
          </el-row>
        </el-col>
      </el-row>
    </template>
 </el-tree>
          
      </el-card>   
    </div>
  </div>
</template>

<script>
import { getDepartments,delDepartment } from "@/api/departments";
import { tranListToTreeData } from "@/utils/index";
import AddorEdit from './deptDialog.vue'

export default {
  created () {
    this.huoquzuzhijiaguo()
  },
  components: {
     AddorEdit 
  },
  data() {
    return {
      // 依赖一份树形数据
      list: [],
      showDialog: false,
      danchuangId:'',
      isEdit: false,
      originList: []
    }
  },
  methods: {
   async huoquzuzhijiaguo(){
      try {
      const res = await getDepartments()
      console.log(res.data.depts);
      res.data.depts.shift()
      this.originList=res.data.depts.map(item => {
        return{
          id:item.id,
          name:item.name,
          pid:item.pid,
          code:item.code
        }

      })
      this.list =tranListToTreeData(res.data.depts) 
      } catch (error) {
        console.log(error);
      }
    },
    async hDel(id) {
    try {
        const confirmResult = await this.$confirm('此操作将永久删除部门, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
        });
        // 用户点击了确定
        const res = await delDepartment(id);
        console.log(res);
        this.$message.success(res.message);
        this.huoquzuzhijiaguo();
    } catch (error) {
        if (error.name === 'CancelError') {
            // 用户点击了取消
            return;
        }
        console.log(error);
        this.$message({ type: 'error', message: '删除失败' });
    }
},
    addzibumen(id){
      // console.log(id,'id是什么');
      this.showDialog= true
      this.danchuangId = id
      console.log(this.danchuangId,'this.danchuangId')
      this.isEdit = false
        //  this.$confirm('确认关闭？')
        //   .then(_ => {
        //     done();
        //   })
        //   .catch(_ => {});
    },
    hEdit(id){
      this.showDialog= true
      this.danchuangId = id
      this.isEdit = true
    },
    guanbidanchuang(){
      this.showDialog= false,
      this.huoquzuzhijiaguo()
    }
  }
}
</script>